<?php
if (!defined('INDEX')) exit;
require("common/functions_jcas_modules.php");
?>
<table width="100%"  border="0" cellspacing="0" cellpadding="2">
  <tr>
    <td class="datatd" bgcolor="#FFFFFF"><?php echo $lang['admin_account_desc']?></td>
  </tr>
</table>
<br />
<?php
// Add new admin account
echo admin_insert();
// Edit admin account
echo admin_update();
// Set value for field
echo common_set_value($tbl['admin_accounts'],'id');
// Delete variable
echo common_delete($tbl['admin_accounts'],'id');
// Delete selected variables
echo common_delete_selected($tbl['admin_accounts'],'id');
// Active selected
echo common_activate_selected($tbl['admin_accounts'],'id','active');
// Deactive selected
echo common_deactivate_selected($tbl['admin_accounts'],'id','active');

if($show_content)
{
?>
		<table width="100%"  border="0" cellspacing="0" cellpadding="0">
	  <tr>
		 <td><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			<tr>
			  <td width="120" class="<?php echo ((@$_GET['p']=='show')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=show"><?php echo $lang['manage_accounts']?></a></td>
			  <td width="5">&nbsp;</td>
			  <td width="120" class="<?php echo ((@$_GET['p']=='add')?'tabtd':'tabtd_blur')?>" align="center"><a href="<?php echo $index_url?>&p=add"><?php echo $lang['add_admin_account']?></a></td>
			  <td width="5">&nbsp;</td>
			  <td >&nbsp;</td>
			</tr>
		 </table></td>
	  </tr>
	  <tr>
		 <td class="tabtd" height="5"></td>
	  </tr>
	</table>
<?php
switch(@$_GET["p"])
{
	case "show":
	default:
		$q_admin = @$DB->query('SELECT * FROM '.$tbl['admin_accounts'].' WHERE created_by!="system" ORDER BY is_super_admin DESC, admin_user ASC');
		$num_admin = $DB->num_rows($q_admin);
		?>

		<table width="100%"  border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
				<form name="frm_admin" method="post" action="<?php echo $index_url?>&p=show">
				  <tr>
					 <td colspan="10" class="headtd"><?php echo $lang["admin_accounts_title"]?></td>
				  </tr>
				  <tr align="center">
					 <td width="30" class="subheadtd"><?php echo $lang['order']?></td>
					 <td width="30" class="subheadtd"><?php echo $lang['id']?></td>
					 <td class="subheadtd"><?php echo $lang['admin_user']?></td>
					 <td width="150" class="subheadtd"><?php echo $lang['last_login']?></td>
					 <td width="80" class="subheadtd"><?php echo $lang['is_super_admin']?></td>
					 <td width="50" class="subheadtd"><?php echo $lang['active']?></td>
					 <td width="80" class="subheadtd"><?php echo $lang['date']?></td>
					 <td width="60" class="subheadtd"><input type="checkbox" name="all_id" class="box" value="all" onClick="set_all_cb('frm_admin','id[]','all_id');" title="<?php echo $lang['check_uncheck_all']?>"></td>
				  </tr>
			<?php
			if($num_admin)
			{
				$no = 0;
				for($x=0;$x<$num_admin;$x++)
				{
					$no += 1;
					$admin = $DB->result_array($q_admin,$x,"id,admin_user,admin_pwd,is_super_admin,privilege,last_login,created_by,created_time,updated_by,updated_time,active");
					
					$admin_title = common_status_title($admin['created_by'], timestamp($admin['created_time']), $admin['updated_by'], timestamp($admin['updated_time'])); 
					$row_color = (!$admin['active'])?'bgcolor="#EEEEEE"':'';
					?>
					<tr  onMouseOver="rollon(this);" onMouseOut="rolloff(this);" <?php echo $row_color?>>
						<td align="right" class="datatd"><?php echo $no?></td>
						<td align="right" class="datatd"><font color="red"><?php echo $admin['id']?></font></td>
						<td class="datatd"><a href="<?php echo $index_url?>&p=edit&id=<?php echo $admin['id']?>" title="<?php echo $admin_title?>"><b><?php echo $admin['admin_user']?></b></a></td>
						<td align="center" class="datatd">
							<?php echo show_date($cfg['date_format'],timestamp($admin['last_login']))?>
						</td>
						<td align="center" class="datatd">
							<?php echo html_yes_no($admin['is_super_admin'],'is_super_admin',$index_url.'&p=show&id='.$admin['id']) ?></td>
						<td align="center" class="datatd">
							<?php echo html_yes_no($admin['active'],'active',$index_url.'&p=show&id='.$admin['id']) ?>
						</td>
						<td align="center" class="datatd">
							<?php echo show_date($cfg['date_format'],timestamp($admin['created_time']))?>
						</td>
						<td align="center" class="datatd">
							<?php echo html_icon_edit($index_url.'&p=edit&id='.$admin['id']) ?>
							<?php echo html_icon_delete($index_url.'&p=show&act=delete&id='.$admin['id']) ?>
							<?php echo html_input_checkbox('id',$admin['id']) ?>
						</td>
				  </tr>
				<?php
				 }
			}
			else 
			{
				?><tr>
					 <td colspan="20" align="center" class="datatd"><?php echo message($lang['no_requested_data'],'info')?></td>
				  </tr>
				<?php
			}
			?>
			<tr>
					 <td colspan="10" align="right" class="row2td">
						<?php echo common_action_list()?>
					 </td>
				  </tr>
				  <input type="hidden" name="session_id" value="<?php echo session_id()?>">
		  </form>
			</table>
		<?php
	break;
		
	case "add":
	case "edit":
		$admin = $DB->fetch_array($DB->query('SELECT * FROM '.$tbl['admin_accounts'].' WHERE id="'.$_GET['id'].'"'));
		if($admin && !$admin['is_super_admin'])
		{
			// Get admin privilege
			// Action: 1=view,2=add,3=edit,4=delete,5=publish
			$admin_arr_page_privi = explode("/",$admin["privilege"]);
			foreach ($admin_arr_page_privi as $admin_arr_id => $admin_page_privi)
			{
				list($admin_vpage,$admin_vprivi) = explode(":",$admin_page_privi);
				$admin_privilege[$admin_vpage] = $admin_vprivi;
				//echo $admin_vpage.'=>'.$admin_vprivi.'<br/>';
			}
		}

		// Edit admin
		if($admin)
			echo js_validate("admin_user","frm_add");
		// Add new admin
		else
			echo js_validate("admin_user,admin_pwd","frm_add");
		?>
		<table width="100%"  border="0" cellspacing="0" cellpadding="0" align="center">
				<form name="frm_add" method="post" action="<?php echo $index_url?>&p=add&id=<?php echo $_GET['id']?>" onSubmit="return validate_frm_add();">
				  <tr>
					 <td colspan="2" class="headtd"><?php echo (($admin)?$lang['edit_admin_account']:$lang['add_new_account'])?></td>
				  </tr>
				  <tr >
					 <td width="30%" class="row1td"><b><?php echo $lang['admin_user']?> *</b> </td>
				  <td width="70%" class="row2td"><input name="admin_user" type="text" size="30" style="width:40%" value="<?php echo (($admin)?$admin["admin_user"]:'')?>" ></td>
				  </tr>
				  <tr >
					 <td class="row1td"><b><?php echo (($admin)?$lang['new_password']:$lang['admin_pwd'].' *')?></b></td>
					 <td class="row2td"><input name="admin_pwd" type="text" size="30" style="width:40%" ></td>
				  </tr>
				  <tr>
					 <td valign="top" class="row1td"><font color="#CC0000"><b><?php echo $lang['is_super_admin']?></b></font></td>
					 <td class="row2td"><input type="checkbox" class="box"  name="is_super_admin" value="yes"  title="Super admin" <?php echo (($admin['is_super_admin'])?'checked=true':'')?>>  <font class="info"><?php echo $lang['is_super_admin_note1']?></font></td>
				  </tr>
				  <tr>
					 <td  class="row1td" valign="top"><b><?php echo $lang['allowed_pages']?></b><br/><font class="info"><?php echo $lang['allowed_pages_note']?></font></td>
				  <td  class="row2td" align="left">
				  <table width="360" border="0" cellspacing="0" cellpadding="2" >
				
				  <tr>
				<td width="5%" class="subheadtd">&nbsp;</td>
				<td width="45%" class="subheadtd"><?php echo $lang['module_name']?></td>
				<td width="10%" align="center" class="subheadtd"><?php echo $lang['action_view']?></td>
				<td width="10%" align="center" class="subheadtd"><?php echo $lang['action_add']?></td>
				<td width="10%" align="center" class="subheadtd"><?php echo $lang['action_edit']?></td>
				<td width="10%" align="center" class="subheadtd"><?php echo $lang['action_delete']?></td>
				<td width="10%" align="center" class="subheadtd"><?php echo $lang['action_publish']?></td>
			  </tr>
			  <tr>
				 <?php
				$loop_key = 0;
				foreach ($module as $module_name=>$module_array)
				{
					$key = 0;
					while (list($module_title,$module_url) = each($module_array))
					{
						if(eregi("menu_title",$module_name))
						{
							?>
							<tr><td class="menu_title" colspan="10"><strong><?php echo $module_title?></strong></td></tr>
							<?php
						}
						if($key == 0 && !eregi("menu_title",$module_name))
						{
							?>
				    <tr>
							<td width="5%" class="row1td"><input type="checkbox" class="box" name="page[<?php echo $module_name?>]" value="<?php echo $module_name?>"  onClick="set_all_cb('frm_add','module[<?php echo $module_name?>][]','page[<?php echo $module_name?>]');" <?php echo (($admin && isset($admin_privilege[$module_name]))?'checked="checked"':'')?>></td>
							<td width="55%" class="row1td" style="color:#0000FF"><?php echo $module_title?></td>
							<td width="10%" align="center" class="row1td"><input type="checkbox" class="box" name="module[<?php echo $module_name?>][]" value="1" onclick="document.frm_add.elements['page[<?php echo $module_name?>]'].checked=true"  <?php echo (($admin && eregi(1,$admin_privilege[$module_name]))?'checked="checked"':'')?>></td>
							<td width="10%" align="center" class="row1td"><input type="checkbox" class="box" name="module[<?php echo $module_name?>][]" value="2" onclick="document.frm_add.elements['page[<?php echo $module_name?>]'].checked=true" <?php echo (($admin && eregi(2,$admin_privilege[$module_name]))?'checked="checked"':'')?>></td>
							<td width="10%" align="center" class="row1td"><input type="checkbox" class="box" name="module[<?php echo $module_name?>][]" value="3" onclick="document.frm_add.elements['page[<?php echo $module_name?>]'].checked=true" <?php echo (($admin && eregi(3,$admin_privilege[$module_name]))?'checked="checked"':'')?>></td>
							<td width="10%" align="center" class="row1td"><input type="checkbox" class="box" name="module[<?php echo $module_name?>][]" value="4" onclick="document.frm_add.elements['page[<?php echo $module_name?>]'].checked=true" <?php echo (($admin && eregi(4,$admin_privilege[$module_name]))?'checked="checked"':'')?>></td>
							<td width="10%" align="center" class="row1td"><input type="checkbox" class="box" name="module[<?php echo $module_name?>][]" value="5" onclick="document.frm_add.elements['page[<?php echo $module_name?>]'].checked=true" <?php echo (($admin && eregi(5,$admin_privilege[$module_name]))?'checked="checked"':'')?>></td>
				    </tr>
							<?php
							$loop_key += 1;
						}
						$key += 1;
					}
				}
				  ?>
				  </table>
				  <hr>
				  <table width="100%"  border="0" cellspacing="0" cellpadding="2">
				 
				  <?php
					 
					 $loop_key = 0;
						foreach ($sub_module as $sub_module_name=>$sub_module_array)
						{
							while (list($sub_module_title,$sub_module_url) = each($sub_module_array))
							{
								if(eregi("jcas",$sub_module_name))
								{
									?>
									 <tr>
									<td width="1%" style="color:#CC0000" ><input type="checkbox" class="box" name="page[<?php echo $sub_module_name?>]" value="<?php echo $sub_module_name?>" <?php echo (($admin && isset($admin_privilege[$sub_module_name]))?'checked="checked"':'')?> ></td>
									<td width="99%" style="color:#CC0000" ><?php echo $sub_module_title?></td>
									 </tr>
									<?php
									$loop_key += 1;
									//if(($loop_key%3)==0) echo '</tr><tr>';
								}
							}
						}
					 ?>
				   </table>
				  </td>
				  </tr>
				  <tr>
					 <td valign="top" class="row1td"><b><?php echo $lang['active']?></b></td>
					 <td class="row2td"><input type="checkbox" class="box"  name="active" value="1"  <?php echo (($admin)?(($admin['active'])?'checked=true':''):'checked=true')?> /></td>
				  </tr>
				  <tr>
					 <td colspan="2" align="center" valign="top" class="row2td">
					 <?php if($admin) { ?>
					 	 <input type="submit" name="update" class="btn_bg btn_update" value="<?php echo $lang['btn_update']?>">
						 <input type="reset"  class="btn_bg btn_reset" value="<?php echo $lang['btn_reset']?>">
					 <?php } else { ?>
					  	<input type="submit" name="insert" class="btn_bg btn_insert" value="<?php echo $lang['btn_insert']?>">
					 <?php } ?>
					
					 </td>
				  </tr>
					 <input type="hidden" name="session_id" value="<?php echo session_id()?>">
		  </form>
				</table>
			<?php
	break;
}// end switch
}// end show_content
$_GET["p"] = false;
?>